package csv_reader

import (
	"bufio"
	"log"
	"os"
	"strings"
	)

// Reads lines from the input buffer and splits each line.
// Ignores empty lines or lines starting with #.
// When the input stream is done, an empty array is emitted.
func ReadCsv(reader *bufio.Reader, sep string) chan []string {
	var (
		outChannel chan []string
		part []byte
		err os.Error
	)
	outChannel = make(chan []string)
		
	go func() {
		for {
			part, _, err = reader.ReadLine()
			if err != nil {
				if err != os.EOF {
					log.Printf("Error reading csv: %s", err.String())
				}
				break
			}
			// Ignore lines that begin with #
			if part[0] == '#' {
				continue
			}
			var items []string
			items = strings.Split(string(part), sep, -1)
			// Ignore empty lines
			if len(items) > 0 {
				outChannel <- items
			}
		}
		outChannel <- []string{}
	}()
	return outChannel
}

